﻿Imports System.Data

Partial Class WebForm_AssetMaintain_Component_AssetEliminate
    Inherits BaseUserControl
    'Public Property mpSearch() As AjaxControlToolkit.ModalPopupExtender
    '    Get
    '        Return ViewState("mpSearch")
    '    End Get
    '    Set(ByVal value As AjaxControlToolkit.ModalPopupExtender)
    '        ViewState("mpSearch") = value
    '    End Set
    'End Property


   

    'Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    '    mpSearch.Show()

    'End Sub
   
    Public Property myDT() As Datatable
        Get
            If ViewState("myDT") Is Nothing Then
                Dim dt As DataTable
                dt = New DataTable()
                dt.Columns.Add("asset_serialNo", System.Type.GetType("System.String"))
                dt.Columns.Add("Def_Name", System.Type.GetType("System.String"))
                dt.Columns.Add("ModiType", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_owner", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_ownerID", System.Type.GetType("System.String"))
                dt.Columns.Add("User2", System.Type.GetType("System.String"))
                dt.Columns.Add("NewOwner", System.Type.GetType("System.String"))
                dt.Columns.Add("NewUser2", System.Type.GetType("System.String"))
                dt.Columns.Add("OtherUser", System.Type.GetType("System.String"))

                ViewState("myDT") = dt
                Return ViewState("myDT")
            Else
                Return ViewState("myDT")
            End If
        End Get
        Set(ByVal value As DataTable)
            ViewState("myDT") = value
        End Set
    End Property

    Public Property myOtherDT() As DataTable
        Get
            If ViewState("myOtherDT") Is Nothing Then
                Dim dt As DataTable
                dt = New DataTable()
                dt.Columns.Add("asset_serialNo", System.Type.GetType("System.String"))
                dt.Columns.Add("Def_Name", System.Type.GetType("System.String"))
                dt.Columns.Add("ModiType", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_owner", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_ownerID", System.Type.GetType("System.String"))
                dt.Columns.Add("User2", System.Type.GetType("System.String"))
                dt.Columns.Add("NewOwner", System.Type.GetType("System.String"))
                dt.Columns.Add("NewUser2", System.Type.GetType("System.String"))
                dt.Columns.Add("OtherUser", System.Type.GetType("System.String"))

                ViewState("myOtherDT") = dt
                Return ViewState("myOtherDT")
            Else
                Return ViewState("myOtherDT")
            End If
        End Get
        Set(ByVal value As DataTable)
            ViewState("myOtherDT") = value
        End Set
    End Property
    Public Sub RemoveData()
        ViewState.Remove("myOtherDT")
        ViewState.Remove("myDT")
        gvMain.DataBind()
    End Sub
    Public Sub IniData(ByVal assetdef_id As String, ByVal Dept As String, ByVal asset_serialNo As String, ByVal dbName As String, ByVal UID As String)
        ViewState("assetdef_id") = assetdef_id
        ViewState("Dept") = Dept
        ViewState("asset_serialNo") = asset_serialNo
        hiDBName.Value = dbName
        ViewState("UID") = UID
        ddlassetdef_id.DataBind()
        ddlassetdef_id.SelectedValue = assetdef_id
        txtDept.Text = Dept
        txtasset_owner.Text = UID
        txtasset_serialNo.Text = asset_serialNo
    End Sub
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        'dbASSET1.SelectParameters("assetdef_id").DefaultValue = ddlassetdef_id.SelectedValue
        btnConfirm_Click(Nothing, Nothing)
        gvMain.DataBind()
        RaiseEvent onShow(sender, New EventArgs())
    End Sub
    Public Event onShowNext(ByVal source As Object, ByVal e As EventArgs)


    Public Event onShow(ByVal source As Object, ByVal e As EventArgs)
 


    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        'btnConfirm_Click(Nothing, Nothing)
    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        btnConfirm_Click(Nothing, Nothing)
        RaiseEvent onShowNext(sender, New EventArgs())
    End Sub

    Protected Sub chkMaintain_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub gvMain_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.PageIndexChanged
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub chkMaintainHeader_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As Boolean = CType(sender, CheckBox).Checked
        For Each gr As GridViewRow In gvMain.Rows
            CType(gr.FindControl("chkMaintain"), CheckBox).Checked = chk
        Next

        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        Dim dr As DataRow
        Dim drs() As DataRow
        Dim drs1() As DataRow

        Dim approveoa As String
        Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        Dim dr2 As DataRow
        For Each gr As GridViewRow In gvMain.Rows
            If CType(gr.FindControl("chkMaintain"), CheckBox).Checked Then

                Try
                    dr2 = uDao.GetAUOFORM87_CODELIST(ddlFactory.SelectedValue, "1").Rows(0)

                    approveoa = dr2("approverScrap").ToString
                Catch ex As Exception
                    ShowMessageForAjax(btnClose, GetLocalResourceObject("errSite"))
                    RaiseEvent onShow(sender, New EventArgs())
                    Return
                End Try

                '檢杳是否已在datable 中
                drs = myDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'")
                If drs.Length = 0 Then
                    drs1 = myOtherDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'") ''檢查是否在移轉資料中
                    If drs1.Length = 0 Then
                        dr = myDT.NewRow()
                        dr("asset_serialNo") = gr.Cells(2).Text ''資產編號
                        dr("Def_Name") = gr.Cells(1).Text  '資產類別
                        dr("ModiType") = "3"  '除帳
                        dr("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
                        dr("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim

                        dr("asset_ownerID") = HttpUtility.HtmlDecode(gr.Cells(9).Text).Trim
                        dr("NewOwner") = approveoa
                        dr("NewUser2") = approveoa
                        myDT.Rows.Add(dr)
                    End If

                End If


            End If
        Next

        ' ShowMessageForAjax(btnConfirm, GetLocalResourceObject("Finish"))


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then


        End If
       
    End Sub

    Protected Sub gvMain_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.DataBound
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub gvMain_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMain.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim asset_serialNo As String = e.Row.Cells(2).Text
            Dim drs() As DataRow = myDT.Select("ModiType='3' and asset_serialNo='" + asset_serialNo + "'")
            If drs.Length > 0 Then
                CType(e.Row.FindControl("chkMaintain"), CheckBox).Checked = True
            End If
        End If


    End Sub

    Protected Sub gvMain_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvMain.PageIndexChanging
        btnConfirm_Click(Nothing, Nothing)
    End Sub

    Protected Sub chkMaintain_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim approveoa As String
        Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        Dim dr2 As DataRow

        If CType(sender, CheckBox).Checked Then
            Dim asset_serialNo As String = CType(sender, CheckBox).ToolTip
            For Each gr As GridViewRow In gvMain.Rows
                If HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim = asset_serialNo.Trim Then

                    Try
                        dr2 = uDao.GetAUOFORM87_CODELIST(ddlFactory.SelectedValue, "1").Rows(0)

                        approveoa = dr2("approverScrap").ToString

                    Catch ex As Exception
                        ShowMessageForAjax(btnClose, GetLocalResourceObject("errSite"))
                        RaiseEvent onShow(sender, New EventArgs())
                        Return
                    End Try




                End If


            Next

        End If
        RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & sender.ClientID & "').focus(); "", 100);")
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub gvMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.SelectedIndexChanged
       
    End Sub
End Class
